<?php
namespace Home\Model;
use Think\Model;
class GoodsModel extends Model {
    protected $trueTableName = 'goods';

    public function get_goods_by_category($cate_id, $offset=0, $sort='like_count', $order_type="ASC", $num = 21)
    {
        $cate_info = D('category')->info($cate_id);
        if(!$cate_info) return array();

        $cate_list     = D('category')->getTree($cate_id);
        $deep_cate_ids = D('category')->get_deep_children_ids($cate_list);
        $map = array('status' => 1);
        $map['category_id'] = array('in', $deep_cate_ids);
        $goods_list = $this->where($map)
                           ->order($sort .' '. $order_type)
                           ->limit($offset, $num)
                           ->select();
        return $goods_list;
    }

    /**
     * 获得用于首页展示的商品
     */
    public function get_index_goods($offset=0, $num=21, $field=true, $sort='like_count', $order_type="DESC")
    {
        $map = array('status' => 1);
        $goods_list = $this->field($field)
                           ->where($map)
                           ->order($sort .' '. $order_type)
                           ->limit($offset, $num)
                           ->select();
        return $goods_list;
    }

    /**
     * 获得用于分类页面展示的商品
     */
    public function get_category_goods_by_ids($cate_ids_str,$offset=0, $num=21, $field=true, $sort='add_time', $order_type="DESC")
    {
        $map = array();
        $map['category_id'] = array('in', $cate_ids_str);
        $map['status']      = 1;

        $goods_list = $this->field($field)
            ->where($map)
            ->order($sort .' '. $order_type)
            ->limit($offset, $num)
            ->select();

        return $goods_list;
    }

    /**
     * 通过商品id获得商品信息
     * @param $goods_id
     */
    public function info($goods_id, $field = true)
    {
        $goods_info = M('goods')->field($field)->find($goods_id);
        if(!$goods_info || $goods_info['status'] != 1) {
            return array();
        } else {
            return $goods_info;
        }
    }

    /**
     * 通过抽屉id获得该抽屉下的对应商品列表
     * @param $drawer_id
     */
    public function get_goods_list_by_drawer_id($drawer_id, $field = true, $start=0, $limit=21, $order='add_time', $sort='DESC')
    {
        $goods_list = M('goods')->field($field)
                                ->where(array('drawer_id'=>$drawer_id, 'status' => 1))
                                ->order($order . ' ' . $sort)
                                ->limit($start .',' .$limit)
                                ->select();
        //获得抽屉发表者的相关信息
        if (count($goods_list)) {
            foreach($goods_list as $k=>$v) {
                $post_user_info = M('user')->field('avatar, user_name')
                                           ->where('id='.$v['post_uid'])
                                           ->find();
                $goods_list[$k]['post_user_name']   = $post_user_info['user_name'];
                $goods_list[$k]['post_user_avatar'] = $post_user_info['avatar'];
            }
        }

        return $goods_list;
    }
}